;
(function (call) {
    var main = {
        exponentCfg: {
            'TT': '总指数', 'KI': 'K指数', 'H0C': '零度高度(米)', 'H-20C': '-20度层高度(米)','TPW': '整层大气可降水量TPW','SWEAT': '强天气威胁指数sweat','LI': '抬升凝结高度(hpa)', 'SI': '沙氏指数',  'TD2M': '2m露点',
            'TMP2M': '2m气温', 'Q700': '比温700', 'Q850': '比温850',  'CAPE': '对流有效位能', 'CIN': '对流抑制有效位能',
            '700-500TMP': '700-500温差', '850-500TMP': '850-500温差'
        },
        router: {
            findPhyConfigList: '/raw/dms/findPhyConfigList',
            findList:'/raw/dms/findList',
            updateCnvciHailNdex: '/raw/dms/save'
        },
        init: function () {
            this.queryCnvciHailNdex();
            this.bindEvt();
        },
        // 查询
        queryCnvciHailNdex: function () {
            var that = this;
            var $body = $('.tableContain table').empty();
            $.ajax({
                url: that.router.findPhyConfigList+"?element="+$("#indexSelect").val(),
                type: 'GET'
            }).done(function (res) {
               let {code,data} = res;
               if(code != 0){
                    alert('查询失败');
                    return;
                }
               let $tr1 = $("<tr>").addClass("head").append($("<td>").text($("#indexSelect").find("option:selected").text()));
               let $tr2 = $("<tr>").addClass("head").append($("<td>").text("判断条件"));
               let trItems = [];
               debugger
                data.sort((item1,item2)=>{
                    return Number(item1.order)-Number(item2.order);
                }).map(item=>{
                    trItems.push(item.id);
                    $tr1.append($("<td>").text(item.name));
                    $tr2.append($("<td>").text(item.relation));
                })
                $body.append($tr1);
                $body.append($tr2);
                $.ajax({
                    url: that.router.findList+"?element="+$("#indexSelect").val(),
                    type: 'GET',
                }).done(function (res) {
                    let {code:listCode,data:listData} = res;
                    if(listCode != 0){
                        alert('查询失败');
                        return;
                    }
                    let formatData = [];
                    for(let i in listData){
                        let singleData = listData[i];
                        let obj = {};
                        obj.month =i;
                        singleData.map(item=>{
                            obj[item.typeIndex] = item.value;
                        })
                        formatData.push(obj)
                    }
                    formatData = formatData.sort((item1,item2)=>{
                        return Number(item1.order)-Number(item2.order);
                    })
                    formatData.map(item=>{
                        let $tr = $("<tr>");
                        $tr.append($("<td>").text(item.month+"月"));
                        trItems.map(key=>{
                            $tr.append($("<td>").data("obj",{month:item.month,typeIndex:key,element:$("#indexSelect").val(),value:item[key]}).append($("<input>").val(item[key]?item[key]:'')))
                        })
                        $body.append($tr);
                    })
                })

            })
        },
        // 保存
        updateCnvciHailNdex: function () {
            var that = this;
            var totalData = [];
            $(".tableContain tr:gt(1)").find("td:gt(0)").each(function (i, td) {
                debugger
                let data = $(td).data("obj");
                data.value = $(td).find("input").val();
                // if(!totalData[data['month']]){
                //     totalData[data['month']] = [];
                // }
                //totalData[data['month']].push(data);
                totalData.push(data);
            })
            $.ajax({
                url: that.router.updateCnvciHailNdex,
                type: "post",
                // contentType: "application/json;charset=utf-8",
                // dataType: 'json',
                data: {
                    list:JSON.stringify(totalData)
                },
                success: function (res) {
                    if (res.isSuccess) {
                        alert('保存成功');
                    } else {
                        alert('保存失败');
                    }
                }
            })
        },
        bindEvt: function () {
            var that = this;
            $("#saveBtn").click(function () {
                that.updateCnvciHailNdex();
            })
            $("#indexSelect").change(function () {
                that.queryCnvciHailNdex();
            })
        }
    };

    if (typeof call === 'function') {
        call(main);
    }

})(function (main) {
    main.init();
});